package com.jason.platform.supervision.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.activity.BaiduMapActivity;
import com.jason.platform.supervision.activity.MonitorActivity;
import com.jason.platform.supervision.adapter.MonitorchildAdapter;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.Category;
import com.jason.platform.supervision.beans.LocateBean;
import com.jason.platform.supervision.beans.LocateResp;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import org.kymjs.aframe.http.KJFileParams;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MonitorChildFragment
 * @Description: 在线监控子视图
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-29 14:03:08
 */
public class MonitorChildFragment extends BaseFragment{

    private static final String TAG = MonitorChildFragment.class.getSimpleName();

    private Category mParentCategory;
    private int mPosition;

    private ListView mListView;
    private MonitorchildAdapter mAdapter;
    private MonitorActivity mActivity;

    private MyHandler<MonitorChildFragment> mHandler;
    private View mFootView;

    private List<LocateBean> mLocateBeans = new ArrayList<LocateBean>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate...");
        mParentCategory = (Category) tag;
        if (arg1 != null) {
            mPosition = (Integer) arg1;
        }
        Log.i(TAG, "onCreate...mPosition：" + mPosition + ",mParentCategory:" + mParentCategory.toString());
        mCurrentIndex = 1;
        mActivity = (MonitorActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView...");
        View view = inflater.inflate(R.layout.fragment_monitor_child, null, true);
        mListView = (ListView) view.findViewById(R.id.monitor_child_list);
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(mActivity, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.GONE);
        mListView.addFooterView(mFootView);
        mAdapter = new MonitorchildAdapter(getActivity());
        mListView.setAdapter(mAdapter);
        mHandler = new MyHandler<MonitorChildFragment>(this);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == (view.getCount() - 1)) {//滚动到底部
                        if (!mIsLoading && mHasNextPage) {
                            loadMore();
                        } else {
                            if (mFootView != null) {
                                mFootView.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume...");
    }

    @Override
    public void onFrameResume() {
        super.onFrameResume();
        Log.i(TAG, "onResume...");
    }

    @Override
    public void initData(int netState) {
        super.initData(netState);
        mIsLoading = true;
        mIsStart = true;
        KJFileParams mParams = new KJFileParams();
        mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        mParams.put("organId", SharedPreferencesUtils.getOrganId(mActivity).toString());
        mParams.put("state", mParentCategory.categoryID);

        Task task = mActivity.getLocateListTask(Constant.Locate.GET_LOCATE_LIST,mParams);
        task.execute();
    }

    private void loadMore() {
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(mActivity, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.VISIBLE);
        mListView.setSelection((mListView.getAdapter().getCount() - 1));
        mIsLoading = true;
        mIsStart = false;

        KJFileParams mParams = new KJFileParams();
        mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        mParams.put("organId", SharedPreferencesUtils.getOrganId(mActivity).toString());
        mParams.put("state", mParentCategory.categoryID);

        Task task = mActivity.getLocateListTask(Constant.Locate.GET_LOCATE_LIST,mParams);
        task.mIsStart = false;
        task.arg2 = mPosition;
        task.execute();

    }

    @Override
    public void onDataCallback(Task task) {
        super.onDataCallback(task);
        if (task.type == Constant.Locate.GET_LOCATE_LIST) {
            setNetInitState(NetState.INIT_SUCCESS);
            mIsStart = task.mIsStart;
            Log.i(TAG,"onDataCallback...mIsStart:" + mIsStart);
            Log.i(TAG,"onDataCallback...isSuccess:" + task.isSuccess);
            if (task.isSuccess) {
                LocateResp resp = (LocateResp) task.resData;
                if (mIsStart){
                    mLocateBeans.clear();
                }
                mLocateBeans.addAll(resp.getValues());
                if (mLocateBeans != null && !mLocateBeans.isEmpty()){
                    if (mAdapter == null){
                        mAdapter = new MonitorchildAdapter(getActivity());
                        mListView.setAdapter(mAdapter);
                    }
                    mAdapter.addAll(mLocateBeans);
                    mAdapter.notifyDataSetChanged();
                }
                mHasNextPage = !resp.isLastPage();
                Log.i(TAG,"onDataCallback...mHasNextPage:" + mHasNextPage);
                if (mHasNextPage) {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.GONE);
                    }
                }
                mIsLoading = false;
                setNetInitState(NetState.INIT_SUCCESS);
            }else {
//                setNetInitState(NetState.INIT_FAILED);
                if (mAdapter != null){
                    mAdapter.addAll(new ArrayList<LocateBean>());
                    mAdapter.notifyDataSetChanged();
                }
                if (mFootView != null) {
                    mFootView.setVisibility(View.GONE);
                }
                Toast.makeText(mActivity, task.rspDesc, Toast.LENGTH_SHORT).show();
            }

        } else if (task.type == Constant.Locate.GET_REAL_TIME_LOCATE){
            MonitorActivity.isLoading = false;
            mActivity.dismissLoading();
            if (task.isSuccess) {
                LocateResp resp = (LocateResp) task.resData;
                //TODO 进入地图
                LocateBean bean = resp.getValues().get(0);
                if (bean.isOpenMap()){
                    Intent intent = new Intent(mActivity, BaiduMapActivity.class);
                    intent.setAction(BaiduMapActivity.ACTION_LOCATE_LOOK);
                    intent.putExtra(BaiduMapActivity.KEY_LOCATE_LOOK,bean);
                    mActivity.startActivity(intent);
                }else {
                    Toast.makeText(mActivity,"暂无坐标信息",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(mActivity,task.rspDesc,Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    void handleMessage(BaseFragment fragment, Message msg) {
        super.handleMessage(fragment, msg);

    }

}
